package notify

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"go-fox-edge-channel-serial-analyzer/channelEntity"
	"go-fox-edge-channel-serial-analyzer/channelService"
)

type operateEntityTypeNotify struct {
}

func (e *operateEntityTypeNotify) Notify(updateTime int64, addMap map[string]commEntity.IEntity, delSet map[string]commEntity.IEntity, mdyMap map[string]commEntity.IEntity) {
	for _, entity := range addMap {
		e.rebindScriptEngine(entity)
	}
	for _, entity := range mdyMap {
		e.rebindScriptEngine(entity)
	}
}

func (e *operateEntityTypeNotify) rebindScriptEngine(entity commEntity.IEntity) {
	operateEntity := entity.(*edgeEntity.OperateEntity)

	channelEntity.Map.Range(func(key, value interface{}) bool {
		serialChannelEntity := value.(*channelEntity.SerialChannelEntity)

		if serialChannelEntity.SplitOperate != nil && serialChannelEntity.SplitOperate.MakeServiceKey() == operateEntity.MakeServiceKey() {
			channelService.RebindScriptEngine(serialChannelEntity, serialChannelEntity.ChannelParam)
		}

		if serialChannelEntity.KeyOperate != nil && serialChannelEntity.KeyOperate.MakeServiceKey() == operateEntity.MakeServiceKey() {
			channelService.RebindScriptEngine(serialChannelEntity, serialChannelEntity.ChannelParam)
		}

		return true
	})
}
